<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>54数组扁平化、去重与排序</title>
</head>
<body>
<script>
  let arr = [40,9,20,4,[1, 2, 3, 4], [2, 3, 4, [1,2,3,[1,2,3]],5], [1, 2, 3, 4], [3, 4, 5, 9]]



  Array.prototype.flatten = function () {
    let _arr = this,
      toStr = {}.toString

    if (toStr.call(_arr) !== "[object Array]"){
      throw new Error('必须是数组')
    }

    let fArr = []
    
    return  _arr.reduce(function (prev,item) {
      let newArr = prev.concat(
        toStr.call(item) === '[object Array]' ? item.flatten() : item
      )
      return Array.from(new Set(newArr)).sort((a,b)=>{
          return a - b
        });
    },[])



    // return fArr = Array.from(new Set(fArr)).sort((a,b)=>{
    //   return a - b
    // });
  }

  console.log(arr.flatten());
</script>
</body>
</html>